package com.yinhe.music.yhmusic.radio;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yinhe.music.common.GlideHelper.GlideHelper;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.TabAdapter;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.model.RadioListInfo;
import com.yinhe.music.yhmusic.radio.contract.IRadioContract;
import com.yinhe.music.yhmusic.radio.presenter.RadioPresenter;
import com.yinhe.music.yhmusic.utils.Preferences;
import com.yinhe.music.yhmusic.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 电台详细界面
 *
 * @author 何晓华
 */
public class RadioActivity extends BaseServiceActivity implements IRadioContract.IRadioView {

    @BindView(R.id.backdrop)
    ImageView mBackdrop;
    @BindView(R.id.collect_state)
    TextView collectState;
    @BindView(R.id.collect_layout)
    LinearLayout collectLayout;
    @BindView(R.id.transparent_toolbar)
    Toolbar toolbar;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.tabs)
    TabLayout tabLayout;
    @BindView(R.id.transparent_toolbar_title)
    TextView toolbarTitle;
    private String[] titles = {"节目", "详情"};
    private int radioId;
    private RadioListInfo radioInfo;
    private boolean mHasSubscribe;

    RadioDetailFragment mRadioDetailFragment;
    RadioPresenter mPresenter;

    @Override
    public void initView() {
        if (getIntent() != null) {
            radioId = getIntent().getIntExtra("radioid", 0);
        }

        hideToolbar();
        collectLayout.setVisibility(View.VISIBLE);
        showQuickControl(true);
        mPresenter.getRadioInfo(radioId);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_singer_detail;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new RadioPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    /**
     * 设置tab
     */
    private void setupViewPager() {
        tabLayout.setupWithViewPager(viewpager);

        RadioSongFragment radioFragment = new RadioSongFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("radioid", radioId);
        radioFragment.setArguments(bundle);

        RadioDetailFragment radioDetailFragment = new RadioDetailFragment();
        bundle.putString("detail", radioInfo.getIntro());
        bundle.putString("anchor", radioInfo.getUserInfo().getNickname());
        bundle.putString("userImg", radioInfo.getUserInfo().getImage());
        radioDetailFragment.setArguments(bundle);
        mRadioDetailFragment = radioDetailFragment;

        List<Fragment> fragmentList = new ArrayList<>();
        titles[0] = titles[0] + radioInfo.getRadioSongNum();
        fragmentList.add(radioFragment);
        fragmentList.add(radioDetailFragment);

        TabAdapter mAdapter = new TabAdapter(getSupportFragmentManager(), fragmentList, titles);
        viewpager.setAdapter(mAdapter);
        ViewUtils.reflexIndicator(tabLayout, 70);
    }

    @Override
    public void setRadioUI(RadioListInfo radioListInfo) {
        radioInfo = radioListInfo;
        setTransparentToolbar(radioInfo.getRadioName());

        mHasSubscribe = radioListInfo.getCollectStatus() != 0;
        initCollectStatus();
        setupViewPager();
        collectLayout.setOnClickListener(v -> setFollow());
        GlideHelper.setImageResource(mBackdrop, radioListInfo.getImage());
    }

    private void setFollow() {
        if (Preferences.getLoginSn().isEmpty()) {
            startLogin();
            return;
        }
        mPresenter.reqCollect(radioId, mHasSubscribe);
    }

    private void initCollectStatus() {

        if (mHasSubscribe) {
            collectState.setText("已订阅");
            Drawable drawable = getResources().getDrawable(R.drawable.ic_has_sub);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            collectState.setCompoundDrawables(drawable, null, null, null);
        } else {
            Drawable drawable = getResources().getDrawable(R.drawable.ic_add_follow);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            collectState.setCompoundDrawables(drawable, null, null, null);
            collectState.setText("订阅");
        }
    }

    @Override
    public void updateCollectUI(boolean collectStatus) {
        mHasSubscribe = collectStatus;
        initCollectStatus();
    }
}
